<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible" width="600px" @close="close">
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="name">
        <el-input v-model.trim="form.name" autocomplete="off" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model.trim="form.phone" autocomplete="off" :disabled="isDisabled" />
      </el-form-item>
      <el-form-item label="工作时间开始" prop="workTimeStar">
        <el-input v-model.trim="form.workTimeStar" autocomplete="off" />
      </el-form-item>

      <el-form-item label="所属省份" prop="provinces">
        <el-select v-model="form.provinces" placeholder="请选择身份" :disabled="isDisabled">
          <el-option v-for="option in propertyList" :key="option.value" :label="option.label" :value="option.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="所属市" prop="cities">
        <el-select v-model="form.cities" placeholder="请选择身份" :disabled="isDisabled">
          <el-option v-for="option in propertyList" :key="option.value" :label="option.label" :value="option.value"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="负责区域" prop="area">
        <el-input v-model.trim="form.area" autocomplete="off" />
      </el-form-item>

      <el-form-item label="详细介绍" prop="detail">
        <el-input v-model.trim="form.detail" autocomplete="off" />
      </el-form-item>

      <el-form-item label="职业类型" prop="type">
        <el-radio-group v-model="form.type">
          <el-radio :label="0">水电</el-radio>
          <el-radio :label="1">通渠</el-radio>
          <el-radio :label="2">开锁</el-radio>
          <el-radio :label="3">家政</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { doEdit } from '@/api/house/room'
  import { getList } from '@/api/house/property'
  import { getFeatureList } from '@/api/house/feature'

  export default {
    name: 'UserManagementEdit',
    data() {
      return {
        form: {
          name: '',
          phone: '',
          workTimeStar: '',
          type: '',
          provincesId: '',
          provinces: '',
          citiesId: '',
          cities: '',
          detail: '',
          area:'',
        },
        title: '',
        dialogFormVisible: false,
        disabled: false,
      }
    },
    created() {
      // 组件加载时获取数据
      this.fetchPropertyOptions()
      this.fetchFeatureOptions()
    },
    methods: {
      showEdit(row) {
        if (!row) {
          this.title = '添加'
          this.disabled = false
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
          this.disabled = true
        }
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg } = await doEdit(this.form)
            this.$baseMessage(msg, 'success')
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      async fetchPropertyOptions() {
        try {
          const response = await getList({ pageSize: 100 }) // 假设接口返回房产名称数据
          this.propertyList = response.data.list.map((item) => ({
            label: item.name,
            value: item.id,
          }))
        } catch (error) {
          console.error('Failed to fetch property names', error)
        }
      },
      async fetchFeatureOptions() {
        try {
          const response = await getFeatureList() // 假设接口返回房产名称数据
          console.log('response', response)
          this.allFeatures = response.data.map((item) => ({
            label: item.feature,
            id: item.id,
            type: item.type,
          }))
        } catch (error) {
          console.error('Failed to fetch property names', error)
        }
        // 初始化 selectedFeatures 对象，确保每种 type 都有一个空数组以存储选中的特色
        Object.keys(this.groupedFeatures).forEach((type) => {
          this.selectedFeatures[type] = []
        })
      },
      getGroupTitle(type) {
        // 根据 type 返回对应的标题
        // 这里假设需要根据每个 type 动态显示不同的标题
        if (type === '0') {
          return '房间配置'
        } else if (type === '1') {
          return '居住要求'
        } else if (type === '2') {
          return '亮点'
        }
      },
    },
    computed: {
      isDisabled() {
        // 根据密码字段是否有值来决定是否禁用输入框
        return this.disabled // 如果有值，则禁用输入框
      },
      groupedFeatures() {
        // 根据 type 分组特色数据
        const grouped = {}
        this.allFeatures.forEach((feature) => {
          if (!grouped[feature.type]) {
            grouped[feature.type] = []
          }
          grouped[feature.type].push({
            id: feature.id,
            feature: feature.label, // 确认使用正确的属性名
            type: feature.type,
          })
        })
        return grouped
      },
    },
  }
</script>
